package com.eco.fanliapp.service.impl;

import com.eco.fanliapp.service.IJdOrderDetailJobService;
import com.eco.fanliapp.service.IJdOrderDetailService;
import com.eco.fanliapp.service.IJdOrderInfoService;
import com.eco.fanliapp.utils.DateCalculateUtils;
import com.eco.fanliapp.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class JdOrderInfoServiceImpl implements IJdOrderInfoService {
    private static final Logger log = LoggerFactory.getLogger(JobOrderInfoHandleNewServiceImpl.class);

    @Autowired
    private IJdOrderDetailJobService jdOrderDetailJobService;

    /**
     * 订单处理方法
     */
    @Override
    public void handleOrder() {
        try {
            List<String> list = handleTime("", 60 * 30);//分钟
            for(String str : list){
                jdOrderDetailJobService.getOrderDetail(1,3,str);
            }
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("*********拉取京东订单错误");
        } finally {
            log.info("*********结束一次拉取京东订单");
        }
    }

    /**
     * 处理时间
     *
     * @param timeFlag
     * @return
     */
    @Override
    public List<String> handleTime(String timeFlag, int minute) {
        //当前日期参照点
        Date currentTime = new Date();
        String currentTimeStr = DateUtils.date2Str(currentTime, null);
        if (timeFlag == null || "".equals(timeFlag)) {
            java.util.Calendar calendar = java.util.Calendar.getInstance();
            calendar.setTime(currentTime);
            calendar.add(java.util.Calendar.MINUTE, -minute);
            timeFlag = DateUtils.date2Str(calendar.getTime(), null);
            log.info("开始订单时间参照点{}", timeFlag);
        }
        //切割时间段(交易类型 M/D/H/N -->每月/每天/每小时/每分钟,多久分成一份，开始时间，结束时间)
        List<String> list = DateCalculateUtils.jdcutDate("F", 1, timeFlag, currentTimeStr);
        return list;
    }

    public static void main(String[] args) {
        JdOrderInfoServiceImpl aa = new JdOrderInfoServiceImpl();
        List<String> strings = aa.handleTime("", 40);
        for(String str : strings){
            System.out.println(str);
        }
    }
}
